package com.codingvip.tree;

import com.codingvip.tree.util.TreePrintUtil;
import com.sun.tools.javac.jvm.Code;

/**
 * @Author Tony.Han
 * @Created at 2020/4/4 11:18 上午
 * Desc:
 */
public class PingHengErChaTree {

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
        public static boolean isBalanced(CodeNode root) {
            if (null == root) {
                return true;
            }
            if(!isBalanced(root.left)){
                return false;
            }
            if(!isBalanced(root.right)){
                return false;
            }
            if(Math.abs(height(root.right) - height(root.left)) > 1){
                return false;
            }


            return true;
        }

        private static  int height(CodeNode root) {
            if (root == null) {
                return 0;
            }
            int height=Math.max(height(root.left), height(root.right)) + 1;
            System.out.println("Node-->"+root.getData()+",height=="+height);
            return height;
        }

    public static void main(String[] args) {
//       BinaryTree binaryTree= TreeMethod.init();
        BinaryTree binaryTree = new BinaryTree();


        CodeNode threetLeft = new CodeNode(6);
        CodeNode threeRight = new CodeNode(7);
        CodeNode fourleft = new CodeNode(8);
        CodeNode fourRight = new CodeNode(9);
        CodeNode twoLeft = new CodeNode(4, fourleft, fourRight);
        CodeNode twotRight = new CodeNode(5);
        CodeNode rootLeft = new CodeNode(2, twoLeft, twotRight);
        CodeNode rootRight = new CodeNode(3, null, null);
        CodeNode root = new CodeNode(1, rootLeft, rootRight);
        binaryTree.setRoot(root);

        //3. 图型显示树结构
        TreePrintUtil.pirnt(binaryTree.getRoot());
        System.out.println(isBalanced(binaryTree.getRoot()));


    }


}
